﻿using Aker.DevTeam;
using DevExpress.XtraEditors;
using Extensions;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Aker
{
    public partial class SentezDetail : XPopupForm
    {
        public SentezDetail()
        {
            InitializeComponent();
        }

        public SentezDetail(string article)
        {
            InitializeComponent();
            txtArtikel.Text = article;          
        }

        private void SentezDetail_Load(object sender, EventArgs e)
        {
            Bind(true);
        }

        private void Bind(bool close)
        {
            DataSet ds = null;
            try
            {
                ds = Data.DMServer.ExecuteDataSet("GetSentezArtikel", txtArtikel.Text);
            }
            catch (Exception ex)
            {
                //XtraMessageBox.Show(ex.ToString());
                ds = Data.DMLocal.ExecuteDataSet("GetSentezArtikelLocal", txtArtikel.Text);
            }

            if (ds.Tables[0].Rows.Count == 0)
            {
                if (close)
                {
                    this.Size = new Size(0, 0);
                    this.Hide();
                    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                }

                XtraMessageBox.Show("Sentez'de ilgili artikelin detayı bulunmamaktadır.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (close)
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                    return;
                }
            }
            else
            {
                DataRow drSK = ds.Tables[0].Rows[0];
                DataTable dt = new DataTable();
                dt.Columns.Add(Language.Sentez.Attribute, typeof(string));
                dt.Columns.Add(Language.Sentez.Value, typeof(string));

                foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    string colName = dc.ColumnName;
                    if (colName == "Article") continue;
                    else if (colName == "Aciklama") colName = Language.Sentez.Aciklama;
                    else if (colName == "StokKartiGramaj") colName = Language.Sentez.Gramaj;
                    else if (colName == "ZeminIpi") colName = Language.Sentez.ZeminIpi;
                    else if (colName == "HavBoyu") colName = Language.Sentez.HavBoyu;
                    else if (colName == "ThumblerIsisi") colName = Language.Sentez.ThumblerIsisi;
                    else if (colName == "MakasGrubu") colName = Language.Sentez.MakasGrubu;
                    else if (colName == "TarakDairesi") colName = Language.Sentez.TarakDairesi;
                    else if (colName == "SIKLIK") colName = Language.Sentez.Siklik;
                    else if (colName == "DesenNo") colName = Language.Sentez.DesenNo;
                    else if (colName == "IsEmriGramaj") colName = Language.Sentez.IsEmriGramaj;
                    else if (colName == "OrguTuru") colName = Language.Sentez.OrgüTuru;
                    else if (colName == "HarmanKilo") colName = Language.Sentez.HarmanKilo;
                    else if (colName == "BantGram") colName = Language.Sentez.BantGram;

                    dt.Rows.Add(colName, drSK[dc].ToStringNN());
                }

                gvSK.Init(dt);
                gvSK.ViewCaption = Language.Sentez.StokKarti;
                gvSK.BestFitColumns();

                gvHammadde.Init(ds.Tables[1]);
                gvHammadde.ViewCaption = Language.Sentez.Hammadde;
                gvHammadde.MakeColumnInvisible("Article");


                gvHammadde.SetCaptions("HammaddeSira:#", "Iplik:" + Language.Sentez.Hammadde, "IplikAciklama:" + Language.Sentez.Aciklama, "IplikMiktar:" + Language.Sentez.Kullanim, "Detay:" + Language.Sentez.Detail);

                gvHammadde.BestFitColumns();

                gvAciklama.Init(ds.Tables[2]);
                gvAciklama.ViewCaption = Language.Sentez.StokKartiDetayi;
                gvAciklama.MakeColumnInvisible("Article");

                gvAciklama.SetCaptions("AciklamaSira:#", "Aciklama:" + Language.Sentez.Aciklama);

                gvAciklama.BestFitColumns();
            }
        }

        private void btnSorgula_Click(object sender, EventArgs e)
        {
            Bind(false);
        }
    }
}
